<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class leaveword_model extends CI_Model {
	
	function __construct(){
        parent::__construct();
    }
    
    /**
     * 添加留言
     */
    function add($data = array()){
    	$this->db->insert('rsh_leave_word', $data);
    	return $this->db->insert_id();
    }	

	/**
	 * 前台留言列表
	 */
	function leaveword_list($data){
		$_where = ' and b.is_del=0 ';
		if(isset($data['where']['user_id']) && $data['where']['user_id'] > 0){
			$_where .= " and b.user_id = ".$data['where']['user_id'];
		}
 		$sql = "SELECT a.username,b.id, b.user_id,b.title,b.content,b.reply_content, b.ftime FROM rsh_user a,rsh_leave_word b WHERE a.user_id=b.user_id {$_where} ORDER BY a.user_id DESC";
		$per_page = $data['per_page'];
 		$page_size = $data['page_size'];
		$sql .= " limit {$per_page},{$page_size}";
		$query = $this->db->query($sql);
		$data['leaveword_list'] = $query->result_array();
		
		//总条数
		$sql_count = "select count(*) as count FROM rsh_user a,rsh_leave_word b WHERE a.user_id=b.user_id {$_where}";
		$query = $this->db->query($sql_count);
		$data['leaveword_count'] = $query->row()->count;
		return $data;	
	}    
	
	/**
	 * 后台留言列表
	 */
	function background_leaveword_list($data) {
		$_select = "a.username,b.id,b.title,b.content,b.ftime";
	    //封装条件
		$_where = " where 1=1 ";
		if(isset($data['where']['username']) && $data['where']['username'] != ''){
			$_where .= " and a.username like '%".$data['where']['username']."%'";
		}
		if(isset($data['where']['title']) && $data['where']['title'] != ''){
			$_where .= " and b.title like '%".$data['where']['title']."%'";
		}
		
		$_limit = " limit ".$data['page_start'].",".$data['page_size'];
		$sql_result = "select $_select FROM rsh_user a inner join rsh_leave_word b on a.user_id=b.user_id and b.is_del = '0'";
		$sql_result .= $_where;
		$sql_result .= $_limit;
		$query = $this->db->query($sql_result);
	
		$data['leaveword_list'] = $query->result_array();
		//总记录条数
		$sql_count = "select count(*) as count FROM rsh_user a inner join rsh_leave_word b on a.user_id=b.user_id";
		$sql_count .= $_where;
		$query = $this->db->query($sql_count);
		$data['leaveword_count'] = $query->row()->count;
		return $data;
	}
	
		/**删除留言
	    * @param unknown_type $id
	    */
	   function remove($id, $data = array()){
	   		$this->db->where('id', $id);
			$this->db->update('rsh_leave_word',$data);
			return $this->db->affected_rows();
	   }
	   
	    /**
	     * 显示留言
	     */
	    function show_leaveword($id){
			$select = "select b.id,a.username,b.title,b.content,b.ftime FROM rsh_user a inner join rsh_leave_word b on a.user_id=b.user_id and b.is_del = '0'";
			$this->db->query($select);
			$this->db->where(array('id'=>$id));
			$query = $this->db->get('rsh_leave_word');
			return $query->row_array();
		}
		
		/**
		 * 显示回复留言
		 */
		function show_reply_leaveword($id) {
			$select = "select b.id,a.username,b.title,b.content,b.ftime,b.reply_content FROM rsh_user a inner join rsh_leave_word b on a.user_id=b.user_id and b.is_del = '0'";
			$this->db->query ($select);
			$this->db->where (array('id' => $id ) );
			$query = $this->db->get( 'rsh_leave_word' );
			return $query->row_array();
		}
		
		/**
		 * 回复留言
		 */
		function reply_leaveword($id,$data = array()) {
			$this->db->where('id', $id);
			$this->db->update('rsh_leave_word', $data);
			return $this->db->affected_rows();
		}	
		
		/**
		 * 显示留言详细信息
		 */
		function leaveword_all_list($id){
		    $sql = "SELECT  a.title,a.content, a.reply_content,a.reply_time FROM rsh_leave_word a,rsh_user b WHERE a.user_id = b.user_id and id= $id";
			$query = $this->db->query($sql);
			return $query->row_array();
		}
}

